<template>
  <div class="app-container">

    <!--设备信息from表单查询信息 -->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">

      <el-form-item :label="$t('store.nearby.name')" prop="name">
        <el-input v-model="queryParams.storeName" :placeholder="$t('store.nearby.placeholder.name')" clearable
          style="width: 240px" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item :label="$t('store.nearby.repairShop')" prop="status">
        <el-select v-model="queryParams.maintenanceShop" :placeholder="$t('store.nearby.placeholder.repairShop')" clearable>
          <el-option v-for="dict in dict.type.repair_shop_status" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
     
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">{{ $t('common.button.query')
        }}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t('common.button.reset') }}</el-button>
      </el-form-item>
    </el-form>

    <!--门店新增入口-->
    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['device:center:add']">{{ $t('common.button.add') }}</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button type="info" plain icon="el-icon-upload2" size="mini" @click="openImport"
          v-hasPermi="['device:center:import']">{{ $t('common.button.import') }}
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['device:center:export']">{{ $t('common.button.export') }}
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['device:center:delete']">{{ $t('common.button.delete') }}
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!--附近门店Table信息-->
    <el-table v-loading="loading" :data="tableData" style="width: 100%" border @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="50" align="center" />
      <el-table-column :label="$t('store.nearby.name')" prop="storeName" width="180"></el-table-column>
      <el-table-column :label="$t('store.nearby.contact')" prop="contactPerson" align="center" width="120"></el-table-column>
      <el-table-column :label="$t('store.nearby.phoneNumber')" prop="contactPhone" align="center" width="120"></el-table-column>
      <el-table-column :label="$t('store.nearby.province')" prop="province" align="center" width="120"></el-table-column>
      <el-table-column :label="$t('store.nearby.city')" prop="city" align="center" width="120"></el-table-column>
      <el-table-column :label="$t('store.nearby.address')" prop="address" align="center" width="180"></el-table-column>
      <el-table-column :label="$t('store.nearby.latitude')" prop="latitude" align="center" width="120"></el-table-column>
      <el-table-column :label="$t('store.nearby.longitude')" prop="longitude" align="center" width="120"></el-table-column>
      <el-table-column :label="$t('store.nearby.shop')" prop="maintenanceShop" align="center" width="120">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.repair_shop_status" :value="scope.row.maintenanceShop" />
        </template>
      </el-table-column>
      <el-table-column :label="$t('store.nearby.importDate')" prop="importTime" align="center" width="120"></el-table-column>
      <el-table-column :label="$t('store.nearby.status')" prop="status" align="center" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.common_status" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column :label="$t('common.table.operate')" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                v-hasPermi="['vehicle:type:remove']">{{ $t('common.button.remove') }}</el-button>
            <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                v-hasPermi="['vehicle:type:edit']">{{ $t('common.button.edit') }}</el-button>
                <el-button size="mini" type="text" icon="el-icon-edit" @click="handleDetail(scope.row)"
                v-hasPermi="['vehicle:type:edit']">{{ $t('common.button.detail') }}</el-button>
        </template>
      </el-table-column>
      <!-- <el-table-column :label="$t('device.center.status')" align="center" prop="status" width="120">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.device_status" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column :label="$t('device.center.bindStatus')" align="center" prop="bindStatus" width="80">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.device_bind_status" :value="scope.row.bindStatus" />
        </template>
      </el-table-column>
      <el-table-column prop="createTime" align="center" :label="$t('device.center.createTime')" width="150">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="bindingTime" align="center" :label="$t('device.center.bindingTime')" width="150">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.bindingTime) }}</span>
        </template>
      </el-table-column> -->
      
    </el-table>

    <!-- 添加或修改附近门店对话框 -->
    <el-dialog :title="isDetail ? $t('store.nearby.detailStore'):$t('store.nearby.editStore')" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item :label="$t('store.nearby.name')"  prop="storeName">
          <el-input v-model="form.storeName" placeholder="请输入设备编码" :disabled="isDetail"/>
        </el-form-item>
        <el-form-item :label="$t('store.nearby.contact')" prop="contactPerson">
          <el-input v-model="form.contactPerson" placeholder="请输入设备编码" :disabled="isDetail"/>
        </el-form-item>
        <el-form-item :label="$t('store.nearby.phoneNumber')" prop="contactPhone">
          <el-input v-model="form.contactPhone" placeholder="请输入设备编码" :disabled="isDetail"/>
        </el-form-item>
        <el-form-item :label="$t('store.nearby.province')" prop="province">
          <el-input v-model="form.province" placeholder="请输入设备ICCID" :disabled="isDetail"/>
        </el-form-item>
        <el-form-item :label="$t('store.nearby.city')" prop="city">
          <el-input v-model="form.city" placeholder="请输入设备蓝牙名称" :disabled="isDetail"/>
        </el-form-item>
        <el-form-item :label="$t('store.nearby.address')" prop="address">
          <el-input v-model="form.address" placeholder="请输入设备蓝牙MAC地址" :disabled="isDetail"/>
        </el-form-item>
        <el-form-item :label="$t('store.nearby.latitude')" prop="latitude">
          <el-input v-model="form.latitude" placeholder="请输入设备协议加密key" :disabled="isDetail"/>
        </el-form-item>
        <el-form-item :label="$t('store.nearby.longitude')" prop="longitude">
          <el-input v-model="form.longitude" placeholder="请输入mcu软件版本" :disabled="isDetail"/>
        </el-form-item>
        <el-form-item :label="$t('store.nearby.shop')" prop="maintenanceShop">
          <el-select v-model="form.maintenanceShop" :placeholder="$t('store.nearby.placeholder.repairShop')" :disabled="isDetail" clearable>
            <el-option v-for="dict in dict.type.repair_shop_status" :key="dict.value" :label="dict.label" :value="dict.value" />
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('store.nearby.status')" prop="status">
          <el-select v-model="form.status" :placeholder="$t('store.nearby.placeholder.repairShop')" :disabled="isDetail" clearable>
            <el-option v-for="dict in dict.type.common_status" :key="dict.value" :label="dict.label" :value="dict.value" />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


    <!--分页-->
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <center-import ref="centerImport" @refreshData="getList"></center-import>
  </div>
</template>

<script>
import { listStore,getStore,addStore,updateStore,delStore } from "@/api/3sStore/data";
import CenterImport from "./centerImport";

export default {
  name: "store",
  components: { CenterImport },
  dicts: ['repair_shop_status', 'common_status'],
  data() {
    return {
      // 表单参数
      form: {},
      deviceTypeOptions: [],
      deviceModelOptions: [],
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      //选中设备的绑定状态数组
      storeStatus:[],
      storeIds: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 设备表格数据
      tableData: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        maintenanceShop: undefined,
        storeName: undefined
      },
      isDetail:false,
      rules: {
        storeName: [
          { required: true, message: "ID不能为空", trigger: "blur" }
        ],
        contactPerson: [
          { required: true, message: "设备编码不能为空", trigger: "blur" }
        ],
        contactPhone: [
          { required: true, message: "设备型号不能为空", trigger: "blur" }
        ],
        province: [
          { required: true, message: "设备类型不能为空", trigger: "change" }
        ],
        city: [
          { required: true, message: "设备加密ID不能为空", trigger: "blur" }
        ],
        address: [
          { required: true, message: "绑定车辆CODE不能为空", trigger: "blur" }
        ],
        latitude: [
          { required: true, message: "设备ICCID不能为空", trigger: "blur" }
        ],
        longitude: [
          { required: true, message: "设备绑定状态：0未绑定，1绑定中，2已解绑不能为空", trigger: "change" }
        ],
        maintenanceShop: [
          { required: true, message: "设备首次登录平台时间不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "设备蓝牙名称不能为空", trigger: "blur" }
        ],
      }
    }
  },
  computed: {},
  watch: {},
  created() {
    this.getList();
  },
  mounted() {
  },
  methods: {
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.storeIds = selection.map(item => item.storeId);
      // this.storeStatus = selection.map(item => item.bindStatus);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    /** 查询附近门店列表 */
    getList() {
      this.loading = true;
      listStore(this.queryParams).then(response =>{
          this.tableData = response.rows;
          this.total = response.total;
          this.loading = false;
      })
     
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    handleUpdate(row){
      this.isDetail = false;
      this.open = ! this.open;
      this.form = row;
      this.form.maintenanceShop = row.maintenanceShop+"";
      this.form.status = row.status+"";
    },
    handleDetail(row){
      this.isDetail = true;
      this.open = ! this.open;
      this.form = row;
      this.form.maintenanceShop = row.maintenanceShop+"";
      this.form.status = row.status+"";
    },
    openImport() {
      this.$refs['centerImport'].init()
    },
    /** 删除按钮操作 */
    handleDelete(row) {
     

      const ids = row.id || this.ids;
      const storeIds = row.storeId || this.storeIds;
      const options = this.$t('common.message.tip.confirm', {
        operate: this.$t('common.operate.type.delete'),
        name: this.$t('device.center.deviceCode'),
        data: storeIds
      })
      this.$modal.confirm(options).then(function () {
        return delStore(storeIds);
      }).then(() => {
        this.getList();
        this.$modal.deleteSuccess();
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      const thiz = this
      const message = this.$t('device.center.exportInfo');
      const title = this.$t('common.prompt.tip');
      this.$confirm(message, title, {
        confirmButtonText: this.$t('common.button.confirm'),
        cancelButtonText: this.$t('common.button.cancel'),
        type: "warning",
      }).then(function () {
        thiz.download('/store/nearby/export', {
          ...thiz.queryParams
        }, `device_${new Date().getTime()}.xlsx`)
      })
    },
   
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.storeId != null) {
            updateStore(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addStore(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        storeId: undefined,
        storeName: undefined,
        status: undefined,
        province: undefined,
        maintenanceShop: undefined,
        longitude: undefined,
        latitude: undefined,
        importTime: undefined,
        contactPhone: undefined,
        contactPerson: undefined,
        city: undefined,
        address: undefined
      };
      this.resetForm("form");
    },
  }
}
</script>
